#include "hikcamera/include/HikCamera.h"
#include "serial/serial.h"
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <iostream>
#include <signal.h>
#include <memory>

// 前向声明
void showUsage(const char* programName);

std::atomic<bool> running(true);
std::atomic<bool> camReady(false);
std::queue<cv::Mat> frameQueue;
std::mutex queueMutex;
std::condition_variable queueCond;

// 串口相关全局变量
std::queue<ReceivedData> receivedDataQueue;
std::mutex dataQueueMutex;
std::condition_variable dataQueueCond;

void cameraThreadFunc(HikCamera& cam);
void serialThreadFunc(Serial& serial);
void armorDetectorThreadFunc();

// 信号处理函数，用于退出
void signalHandler(int signum) {
    std::cout << "Interrupt signal (" << signum << ") received." << std::endl;
    running = false;
}

int main(int argc, char* argv[])
{
    // 检查命令行参数
    if (argc > 1) {
        std::string mode = argv[1];
        if (mode == "--help" || mode == "-h") {
            showUsage(argv[0]);
            return 0;
        } else if (mode == "calibrate") {
            std::cout << "请运行独立的校准程序: ./calibrate_camera" << std::endl;
            return 0;
        } else if (mode == "test-calibration") {
            std::cout << "请运行独立的校准测试程序: ./test_calibration" << std::endl;
            return 0;
        } else {
            std::cerr << "未知参数: " << mode << std::endl;
            showUsage(argv[0]);
            return -1;
        }
    }

    // 注册信号处理函数
    signal(SIGINT, signalHandler);

    // 初始化相机
    HikCamera cam;
    std::thread camThread(cameraThreadFunc, std::ref(cam));

    // 初始化串口
    Serial serial;
    if (!serial.init("config/serial.json")) {
        std::cerr << "Failed to initialize serial communication." << std::endl;
        return -1;
    }

    // 只有在非虚拟串口模式下才启动串口线程
    std::unique_ptr<std::thread> serialThread;
    if (!serial.isVirtual()) {
        serialThread = std::make_unique<std::thread>(serialThreadFunc, std::ref(serial));
    }

    // 启动装甲板检测线程
    std::thread armorDetectorThread(armorDetectorThreadFunc);

    // 主循环
    while (running.load()) {
        // 主循环可以处理其他任务
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    // 等待所有线程结束
    camThread.join();

    // 只有在非虚拟串口模式下才等待串口线程结束
    if (!serial.isVirtual() && serialThread) {
        serialThread->join();
    }

    // 等待装甲板检测线程结束
    armorDetectorThread.join();

    std::cout << "exit." << std::endl;
    return 0;
}

void showUsage(const char* programName) {
    std::cout << "使用方法: " << programName << " [选项]" << std::endl;
    std::cout << "选项:" << std::endl;
    std::cout << "  (无参数)        : 运行正常模式" << std::endl;
    std::cout << "  --help, -h      : 显示此帮助信息" << std::endl;
    std::cout << "  calibrate       : 相机校准模式 (请运行 ./calibrate_camera)" << std::endl;
    std::cout << "  test-calibration: 测试校准结果 (请运行 ./test_calibration)" << std::endl;
    std::cout << std::endl;
    std::cout << "独立程序:" << std::endl;
    std::cout << "  ./calibrate_camera    : 执行相机校准" << std::endl;
    std::cout << "  ./test_calibration    : 测试校准结果" << std::endl;
}
